package com.comboo.example.web.controller;

import com.comboo.example.web.api.ResponseWrapper;
import com.comboo.example.web.exception.ErrorType;
import com.comboo.example.web.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Sherman
 * created in 2018/12/19
 */

@Slf4j
@ControllerAdvice
public class ExceptionController {

    @ResponseBody
    @ExceptionHandler(value = ServiceException.class)
    public ResponseWrapper<Void> serviceErrorHandler(ServiceException ex) {
        ex.printStackTrace();
        ResponseWrapper<Void> resp = ResponseWrapper.error(ex.getErrorType());
        if (ex.getMessage() != null) {
            resp.setMessage(ex.getMessage());
        }
        return resp;
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseWrapper<Void> exceptionHandler(Exception ex) {
        ex.printStackTrace();
        ResponseWrapper<Void> resp = ResponseWrapper.error(ErrorType.UNKNOW_ERROR);
        return resp;
    }

}
